# -*- coding: utf-8 -*-
# @Time    : 2021-10-06 19:23
# @Author  : 十三
# @Email   : 2429120006@qq.com
# @File    : SMSVerify.py
# @Software: PyCharm
import ast
import random

import requests


class SMS_verify:
    def __init__(self, **kwargs):
        self.URL = None
        self.Header = None
        self.params = {"content": '','mobile': ''}
        self.URL, self.Header = self.InitConfig()

    def RandomNumber(self):
        """
        :return: number
        """
        # 生成随机整数0-9
        number = ''
        for i in range(6):
            number += str(random.randint(0, 9))
        return number

    def SMSLoginVerify(self, **kwargs):
        res = {'code': None, 'msg': ''}
        code = self.RandomNumber()
        # 手机号
        result = self.ReturnCode(code=code, phone=kwargs.get('phone'))
        # b'{"ReturnStatus":"Success","Message":"ok",
        # "RemainPoint":1495615,"TaskID":74267427,"SuccessCounts":1}'
        if result.get('ReturnStatus') == 'Success':
            res.update(code=200, msg='发送成功')
            return res
        res.update(code=3001, msg='发送失败')
        return res

    # 返回验证码
    def ReturnCode(self, **kwargs):
        """
        :param content: 短信通知验证码模板内容
        :param phone: 手机号
        :return: 返回结果
        """
        content = "【创信】你的验证码是：{}，3分钟内有效！".format('5873')
        print(content)
        self.params.update(content=content, mobile=kwargs.get("phone"))
        print(self.params)
        r = requests.request("GET", url=self.URL, params=self.params, headers=self.Header)

        # bytes转字符串再转json
        print(r.content)
        r = ast.literal_eval(str(r.content, encoding='utf-8'))
        return r

    def InitConfig(self):
        """
        :return: URL 请求地址
        headers 请求头
        """
        # 短信验证URL
        URL = 'http://gwgp-wtxhytukujk.n.bdcloudapi.com/chuangxin/dxjk'
        # 请求头
        headers = {

            'Content-Type': 'application/json;charset=UTF-8',
            'X-Bce-Signature': 'AppCode/71568e81df9d44caaf9752ab65abfd90'
        }
        return URL, headers


# if __name__ == '__main__':
#     test = SMS_verify()
#     re = test.SMSLoginVerify(phone='19994402296')
#     print(re.get('msg'))
